import {Restivus} from 'meteor/nimble:restivus';
import {Meteor} from 'meteor/meteor';
import { Random } from 'meteor/random';
import {DeviceLogs} from '../imports/api/device_logs';
if (Meteor.isServer) {
    Meteor.startup(() => {
        let Api = new Restivus({
            useDefaultAuth: true,
            prettyJson: true,
        });
       
        //锁操作记录列表
        Api.addRoute('getDeviceLogs', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const logintoken = param.token;
                        const deviceId = param.deviceId;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":logintoken});
                        // if(user) {
                            const logs = DeviceLogs.find({deviceId: deviceId}).fetch();
                            if(logs.length > 0){
                                logs.forEach((log) => {
                                    log.openTime = new Date(new Date(log.openTime).getTime()+1000*60*60*8);
                                });
                                console.log(logs);
                                return {success: true, result: logs, resultCode: 1000}
                            }else{
                                return {success: false, result: [], resultCode: 1002}
                            }
                        // }else{
                        //     return {success: false, result: [], resultCode: 1004};
                        // }
                    }
                    return {success: false, result: [], resultCode: 1002};
                }
            }
        });
    });

} 